package com.lyc.mapper;

import com.lyc.pojo.CommentLikes;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

@Mapper
public interface CommentLikesMapper {
    
    /**
     * 插入评论点赞记录
     * @param commentLikes 评论点赞对象
     * @return 影响行数
     */
    @Insert("INSERT INTO comment_likes (comment_id, user_id, created_at) VALUES (#{commentId}, #{userId}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(CommentLikes commentLikes);
    
    /**
     * 根据评论ID和用户ID删除点赞记录
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 影响行数
     */
    @Delete("DELETE FROM comment_likes WHERE comment_id = #{commentId} AND user_id = #{userId}")
    int deleteByCommentIdAndUserId(@Param("commentId") Integer commentId, @Param("userId") Integer userId);
    
    /**
     * 根据评论ID和用户ID查询点赞记录
     * @param commentId 评论ID
     * @param userId 用户ID
     * @return 评论点赞对象
     */
    @Select("SELECT * FROM comment_likes WHERE comment_id = #{commentId} AND user_id = #{userId}")
    CommentLikes findByCommentIdAndUserId(@Param("commentId") Integer commentId, @Param("userId") Integer userId);
    
    /**
     * 根据评论ID查询点赞数量
     * @param commentId 评论ID
     * @return 点赞数量
     */
    @Select("SELECT COUNT(*) FROM comment_likes WHERE comment_id = #{commentId}")
    int countByCommentId(@Param("commentId") Integer commentId);
    
    /**
     * 根据评论ID列表查询点赞数量
     * @param commentIds 评论ID列表
     * @return 点赞数量列表
     */
    @Select({
        "<script>",
        "SELECT comment_id, COUNT(*) as like_count FROM comment_likes",
        "WHERE comment_id IN",
        "<foreach collection='commentIds' item='commentId' open='(' separator=',' close=')'>",
        "#{commentId}",
        "</foreach>",
        "GROUP BY comment_id",
        "</script>"
    })
    List<CommentLikes> countByCommentIds(@Param("commentIds") List<Integer> commentIds);
    
    /**
     * 根据评论ID列表和用户ID查询用户点赞的评论
     * @param commentIds 评论ID列表
     * @param userId 用户ID
     * @return 用户点赞的评论ID集合
     */
    @Select({
        "<script>",
        "SELECT comment_id FROM comment_likes",
        "WHERE user_id = #{userId} AND comment_id IN",
        "<foreach collection='commentIds' item='commentId' open='(' separator=',' close=')'>",
        "#{commentId}",
        "</foreach>",
        "</script>"
    })
    Set<Integer> findUserLikedComments(@Param("commentIds") List<Integer> commentIds, @Param("userId") Integer userId);
}